'use strict';
/**
 * Created by FreezeSoul on 2015/10/10.
 */
define([
        'angular',
        'adConfig',
        'app/modules/index/define'
    ],
    function (angular, adConfig) {
        angular.module('ad.index')
            .factory('adIndexEditService', ['$http', '$q', '_', 'adRequest', function ($http, $q, _, adRequest) {

                function getPage(current, size, search) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'chart/chart/search';
                    adRequest(url, {
                        criteria: search, pageIndex: current, pageSize: size
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function getCharts() {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'chart/chart/get';
                    adRequest(url, {}).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function addChart(item) {
                    var chartName = item.chart_name;
                    var folderId = item.chart_folder_id;
                    var chartTypeId = item.chart_type_id;
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'chart/chart/add';
                    adRequest(url, {
                        chartName: chartName,
                        folderId: folderId,
                        chartTypeId: chartTypeId
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function removeCharts(items) {
                    var chartIds = _.map(items, function (item) {
                        return item.chart_id;
                    });
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'chart/chart/delete';
                    adRequest(url, {
                        chartIds: angular.toJson(chartIds)
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function loadChart(chartId) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'chart/chart/load';
                    adRequest(url, {
                        chartId: chartId
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function updateChart(item) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'chart/chart/update';
                    adRequest(url, {
                        chartId: item.chart_id,
                        chartName: item.chart_name,
                        chartTypeId: item.chart_type_id,
                        chartData: item.chart_data,
                        folderId: item.chart_folder_id
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function loadDataSet(dataSetCodes, params) {
                    if (!angular.isArray(params)) params = [];
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'index/chart/data';
                    adRequest(url, {
                        dataSetCodes: angular.toJson(dataSetCodes),
                        params: angular.toJson(params)
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function loadPageParams(pageId) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'index/page/params';
                    adRequest(url, {
                        pageId: pageId
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function folderCreate(folderPath, folderName) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'common/resource/folder/create';
                    adRequest(url, {
                        folderPath: folderPath,
                        folderName: folderName
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function folderDelete(folderPath, folderName) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'common/resource/folder/delete';
                    adRequest(url, {
                        folderPath: folderPath,
                        folderName: folderName
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function folderRename(folderPath, folderName, folderNewName) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'common/resource/folder/rename';
                    adRequest(url, {
                        folderPath: folderPath,
                        folderName: folderName,
                        folderNewName: folderNewName
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function folderList(folderPath) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'common/resource/folder/list';
                    adRequest(url, {
                        folderPath: folderPath
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function fileList(folderPath) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'common/resource/file/list';
                    adRequest(url, {
                        folderPath: folderPath
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function fileDelete(folderPath, fileName) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'common/resource/file/delete';
                    adRequest(url, {
                        folderPath: folderPath,
                        fileName: fileName
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                return {
                    getPage: getPage,
                    loadChart: loadChart,
                    getCharts: getCharts,
                    removeCharts: removeCharts,
                    addChart: addChart,
                    updateChart: updateChart,
                    loadDataSet: loadDataSet,
                    loadPageParams: loadPageParams,

                    folderCreate: folderCreate,
                    folderDelete: folderDelete,
                    folderRename: folderRename,
                    folderList: folderList,
                    fileList: fileList,
                    fileDelete: fileDelete

                };
            }]);
    });